<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <div>
      <!-- 列表 -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
      >
        <ArticleItem
          v-for="obj in articlList"
          :key="obj.art_id"
          :artObj="obj"
          :isShow="false"
          @click.native="itemClickFn(obj.art_id)"
        ></ArticleItem>
      </van-list>
    </div>
  </div>
</template>

<script>
import { searchResultAPI } from '@/api'
import ArticleItem from '@/components/ArticleItem'
export default {
  name: 'SearchResult',
  data () {
    return {
      page: 1,
      articlList: [],
      loading: false,
      finished: false
    }
  },
  components: {
    ArticleItem
  },
  async created () {
    const res = await searchResultAPI({
      page: this.page,
      q: this.$route.params.keywords
    })
    this.articlList = res.data.data.results
  },
  methods: {
    async onLoad () {
      if (this.articlList.length > 0) {
        this.page++
        const res = await searchResultAPI({
          page: this.page,
          q: this.$route.params.keywords
        })
        if (res.data.data.results.length === 0) {
          this.finished = true
          return
        }
        this.articlList = [...this.articlList, ...res.data.data.results]
        this.loading = false
      }
    },
    itemClickFn (id) {
      this.$router.push({
        path: `/detail?art_id=${id}`
      })
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
